import { _decorator, Animation, Collider, Component, Enum, ICollisionEvent, instantiate, Node, NodeEventType, Prefab, RigidBody, SkeletalAnimation, v3, Vec3 } from 'cc';
import { Barrier } from './Barrier';
import { ENEMYPATTERN, Group } from './GameConstant';
import { GameManager } from './GameManager';
import { Label3D } from '../../1/Label3D';
import { ResourceUtil } from '../AshFramework/Utils/ResourceUtil';
import { PoolManager } from '../AshFramework/Managers/PoolManager';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
import { BundleManager, Bundles } from '../AshFramework/Managers/BundleManager';
const { ccclass, property } = _decorator;

export enum Ani {
    Idle = "Idle",
    LeftTake = "LeftTake",
    RightTake = "RightTake",
    Hurt = "Hurt",
}

const boundaryX: number = 0.032;

@ccclass('Enemy')
export class Enemy extends Barrier {

    @property({ type: Enum(ENEMYPATTERN) })
    Pattern: ENEMYPATTERN = ENEMYPATTERN.STAND;

    @property
    Hp: number = 10;

    @property(Label3D)
    Label3D: Label3D = null;

    @property
    Speed: number = 0.1;

    SkeletalAnimation: SkeletalAnimation = null;
    State: Ani = Ani.Idle;
    dirX: number = 1;
    isHurt: boolean = false;

    protected onLoad(): void {
        super.onLoad();
        this.SkeletalAnimation = this.node.getComponent(SkeletalAnimation);
        this.Collider.on(`onCollisionStay`, this.onCollisionStay, this);
        if (this.Pattern == ENEMYPATTERN.MOVE) {
            this.SkeletalAnimation.on(Animation.EventType.FINISHED, this.onAnimationFinished, this);
        }
    }

    protected start(): void {
        GameManager.Instance.EnemyTs = this;
        this.Label3D.string = this.Hp.toString();
    }

    onAnimationFinished() {
        this.isHurt = false;
        this.SkeletalAnimation.play(this.State);
    }

    protected update(dt: number): void {
        if (this.Pattern == ENEMYPATTERN.MOVE) {
            this.move(dt);
        }
    }

    onCollisionStay(event: ICollisionEvent) {
        if (event.otherCollider.getGroup() == Group.Bullet) {
            PoolManager.PutNode(event.otherCollider.node);
            this.hurt();
        } else if (event.otherCollider.getGroup() == Group.Weapon) {
            GameManager.Instance.repulsed();
        }
    }

    hurt() {
        AudioManager.PlaySound(Audios.HitEnemy);
        this.SkeletalAnimation.play(Ani.Hurt);
        this.Hp -= GameManager.Instance.Harm;
        this.isHurt = true;
        this.showHP();
        if (this.Hp <= 0) {
            BundleManager.LoadPrefab(Bundles.Prefabs, "Barrier/钞票").then((prefab: Prefab) => {
                const node: Node = instantiate(prefab);
                node.parent = this.node.parent;
                node.setWorldPosition(this.node.getWorldPosition().clone());
                this.node.destroy();
            })
        }
    }

    showHP() {
        if (this.Hp <= 0) this.Hp = 0;
        this.Label3D.string = this.Hp.toString();
    }

    move(dt: number) {
        if (this.isHurt) return;
        if (this.node.position.x <= -boundaryX && this.State != Ani.RightTake) {
            this.State = Ani.RightTake;
            this.SkeletalAnimation.play(this.State);
            this.dirX = 1;
        } else if (this.node.position.x >= boundaryX && this.State != Ani.LeftTake) {
            this.State = Ani.LeftTake;
            this.SkeletalAnimation.play(this.State);
            this.dirX = -1;
        }
        this.node.setPosition(this.node.getPosition().clone().add3f(dt * this.dirX * this.Speed, 0, 0));
    }

}


